﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using JN.Utility;

namespace Power.Config
{
    public class SystemConfig
    {
        private static readonly object _lockObj = new object();
        private static SystemConfig _instance = null;
        public static SystemConfig Instance
        {
            get
            {
                lock (_lockObj)
                {
                    if (_instance == null)
                    {
                        _instance = new SystemConfig();
                    }
                    return _instance;
                }
            }
        }
        public SystemConfig()
        {
            _dbIp = "127.0.0.1";
            _schema = "power_db";
            _dbUid = "sa";
            _dbPwd = "";
        }
        #region property        
        private string _dbIp;
        private string _schema;
        private string _dbUid;
        private string _dbPwd;

        public string DbIp
        {
            get { return _dbIp; }
            set { _dbIp = value; }
        }
        public string Schema
        {
            get { return _schema; }
            set { _schema = value; }
        }
        public string DbUid
        {
            get { return _dbUid; }
            set { _dbUid = value; }
        }
        public string DbPwd
        {
            get { return _dbPwd; }
            set { _dbPwd = value; }
        }
        #endregion

        #region method
        public bool LoadConfig(string file)
        {
            try
            {
                SystemConfig sysConfig;

                XmlSerializer serial = new XmlSerializer(typeof(SystemConfig));               
                if (File.Exists(file))
                {
                    FileStream reader = new FileStream(file, FileMode.Open);
                    // Call the Deserialize method and cast to the object type.
                    sysConfig = (SystemConfig)serial.Deserialize(reader);
                    reader.Close();
                }
                else
                {
                    sysConfig = SystemConfig.Instance;
                }
                SystemConfig systemConfig = SystemConfig.Instance;
                systemConfig.DbIp = sysConfig.DbIp;
                systemConfig.DbPwd = sysConfig.DbPwd;
                systemConfig.DbUid = sysConfig.DbUid;
                systemConfig.Schema = sysConfig.Schema;

                return true;
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
                return false;
            }
        }
        public string GetConnString()
        {
            StringBuilder connString = new StringBuilder();
            connString.Append("server="+_dbIp);
            connString.Append(";database="+_schema);
            connString.Append(";uid="+_dbUid);
            connString.Append(";pwd="+Common.DEncrypt.DEncrypt.Decrypt(_dbPwd));

            return connString.ToString();
        }
        #endregion
    }
}
